package com.lzx.applib.adapter;

import java.lang.reflect.Constructor;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

public abstract class BAdapter extends BaseAdapter {

	private Adapter<ViewHolder> recyclerAdapter = null;

	public abstract void onBindViewHolder(ViewHolder holder, int position);

	public abstract ViewHolder onCreateViewHolder(ViewGroup parent, int viewType);
	
	public abstract int getItemCount();

	public int getViewTypeCount() {
		return 1;
	}
	
	public int getItemViewType(int position) {
		return 0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public final int getCount() {
		return getItemCount();
	}
	
	@Override
	public final View getView(final int position, View convertView, ViewGroup parent) {
		int type = getItemViewType(position);
		ViewHolder holder = null;
		if (convertView == null) {
			holder = onCreateViewHolder(parent, type);
			holder.itemView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
        setItemClick(holder, position);
		onBindViewHolder(holder, position);
		
		return holder.itemView;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		if (recyclerAdapter != null) {
			recyclerAdapter.notifyDataSetChanged();
		}
	}
	
	public final static <T> T createViewHolder(ViewGroup parent, LayoutInflater inflater, int resId, Class<? extends ViewHolder> cls) {
		try {
			View view = inflater.inflate(resId, parent, false);
			Constructor<?> constructor = (Constructor<?>) cls.getConstructor(View.class);
			T t = (T) constructor.newInstance(view);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Adapter<ViewHolder> toRecyclerAdapter() {
		if (recyclerAdapter != null) return recyclerAdapter;
		
		recyclerAdapter = new Adapter<RecyclerView.ViewHolder>() {

			@Override
			public int getItemCount() {
				return BAdapter.this.getItemCount();
			}

			@Override
			public void onBindViewHolder(ViewHolder holder, int position) {
                setItemClick(holder, position);
                BAdapter.this.onBindViewHolder(holder, position);
			}

			@Override
			public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
				ViewHolder holder = BAdapter.this.onCreateViewHolder(parent, viewType);
                holder.itemView.setTag(holder);
                return holder;
            }
			
			@Override
			public long getItemId(int position) {
				return BAdapter.this.getItemId(position);
			}
			
			@Override
			public int getItemViewType(int position) {
				return BAdapter.this.getItemViewType(position);
			}
		};
		return recyclerAdapter;
	}


    protected AdapterView.OnItemClickListener itemClickListener;
    protected AdapterView.OnItemLongClickListener itemLongClickListener;

    public void setOnItemClickListener(AdapterView.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }

    private void setItemClick(ViewHolder holder, final int position) {
        if (itemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClickListener.onItemClick(null, v, position, getItemId(position));
                }
            });
        }

        if (itemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    itemLongClickListener.onItemLongClick(null, v, position, getItemId(position));
                    return true;
                }
            });
        }
    }
}
